#!/bin/bash
set -euo pipefail
set -x
WORKDIR=$(pwd)
SKIPME='false'

cleanup_vagrant_leftovers() {
  echo "Cleaning up Vagrant leftovers (if any)"
  set +e
  # TODO(cmaloney): Move to killing in virtualbox dirrectly? (rm -rf "~/VirtualBox VMs")
  vagrant destroy -f

  set +e
  for box in $( vboxmanage list vms | grep .dcos | cut -d '{' -f 2 | tr -d '}' ); do
    vboxmanage controlvm "$box" poweroff
    vboxmanage unregistervm "$box" --delete;
  done
  set -e

  vagrant global-status --prune
  set -e
}

echo "Fetching dcos-vagrant"

# Get the dcos-vagrant
# TODO(cmaloney): Have TeamCity do this for us.
# If it already exists, fetch, otherwise clone
if [ ! -d dcos-vagrant ]; then
  git clone https://github.com/dcos/dcos-vagrant
else
  git -C dcos-vagrant fetch origin -t +refs/heads/*:refs/remotes/origin/*
fi

# Pin to just the right dcos-vagrant commit
git -C dcos-vagrant checkout -qf 03de5522ba6da57b3f624fcd9985545f70a89497

cp ../dcos_generate_config.sh dcos-vagrant/dcos_generate_config.sh

cd dcos-vagrant

# Make sure we're running inside the test directory which contains the Vagrantfile
if [ ! -f Vagrantfile ]
then
  echo "Expected a Vagrantfile inside the dcos-vagrant repo"
  exit 1
fi

# Flag used so TeamCity can tell if the build passed or failed. On failure,
# TeamCity will get logs from vagrant if possible then cleanup all artifacts.
rm -rf PASSED

export MASTER_IP=192.168.65.90
export DCOS_DNS_ADDRESS=http://$MASTER_IP
export MASTER_HOSTS=192.168.65.90,192.168.65.95,192.168.65.101

cp VagrantConfig-3m-6a-3p.yaml VagrantConfig.yaml
export DCOS_INSTALL_METHOD="ssh_pull"
export DCOS_CONFIG_PATH="etc/3_master.yaml"
export DCOS_PRIVATE_REGISTRY="true"
export DCOS_GENERATE_CONFIG_PATH="dcos_generate_config.sh"
cat <<EOF > "$DCOS_CONFIG_PATH"
---
cluster_name: test_cluster
bootstrap_url: http://boot.dcos
dns_search: mesos
exhibitor_storage_backend: zookeeper
exhibitor_zk_hosts: 192.168.65.50:2181
exhibitor_zk_path: /zk-shared
master_discovery: static
check_time: 'false'
enable_lb: 'false'
master_list:
  - 192.168.65.90
  - 192.168.65.95
  - 192.168.65.101
oauth_enabled: 'false'
platform: vagrant-virtualbox
EOF

cleanup_vagrant_leftovers

echo "Starting Vagrant VMs"

vagrant up m1 m2 m3 a1 a2 p1 boot

# CWD will be mounted as /vagrant, so write locally and trigger via vagrant ssh
cat <<EOF > test_wrapper.sh
#!/bin/bash
source /opt/mesosphere/environment.export
export DCOS_DNS_ADDRESS=$DCOS_DNS_ADDRESS
export MASTER_HOSTS=$MASTER_HOSTS
export SLAVE_HOSTS=192.168.65.111,192.168.65.121
export PUBLIC_SLAVE_HOSTS=192.168.65.60
export TEAMCITY_VERSION="${TEAMCITY_VERSION:-}"
export DCOS_DEFAULT_OS_USER=root
cd /opt/mesosphere/active/dcos-integration-test
/opt/mesosphere/bin/dcos-shell py.test -vv -rs ${CI_FLAGS:-}
EOF
chmod +x test_wrapper.sh
echo "Running integration test"
set +e # So we can potentially capture the logs
vagrant ssh m1 -c /vagrant/test_wrapper.sh
RET=$?

if [ $RET -ne 0 ]; then
  echo "TEST FAILED"
  echo "Attempting to gather logs"
  export LOG_REPLAY_LINES=${LOG_REPLAY_LINES:=20}
  for i in boot m1 m2 m3 a1 a2 p1; do
    if [ "$TEAMCITY_BUILD_ENV" == "true" ]; then
      echo "##teamcity[blockOpened name='$i']"
      vagrant ssh $i -c "'sudo journalctl -n $LOG_REPLAY_LINES'"
      echo "##teamcity[blockClosed name='$i']"
    else
      vagrant ssh $i -c "'sudo journalctl -n $LOG_REPLAY_LINES'"
    fi
  done
  if [ "$TEAMCITY_BUILD_ENV" == "true" ]; then
    exit 0
  else
    exit 1
  fi
fi

echo "Cleaning up vagrant"
vagrant destroy -f
set -e

touch PASSED
